<?php defined('SYSPATH') OR die('No direct access allowed.');
class code_Core
{
	public static function getBrush($lan)
	{
		$return = $lan = strtolower($lan);
		$allLan = array_map('strtolower',Kohana::config('language.lan'));
		if(!in_array($lan,$allLan)) return 'plain';//如果不是预定义的语言，返回文本格式
		switch($lan)
		{
			case 'c++':
				$return = 'cpp';
				break;
			case 'c#':
				$return = 'csharp';
				break;
			case 'actionscript':
				$return = 'as3';
				break;
			case 'bash/shell':
				$return = 'bash';
				break;
			case 'javascript':
				$return = 'jscript';
				break;
			case 'visual basic':
				$return = 'vb';
				break;
			case 'other':
				$return = 'plain';
				break;
		}
		return $return;
	}

	public static function getBrushFile($lan)
	{
		$filePath = 'media/script/syntax/';
		$file = 'shBrush'.ucfirst($lan).'.js';
		$file1 = 'shBrush'.ucfirst(substr($lan,0,1)).ucfirst(substr($lan,1)).'.js';
		if(file_exists(DOCROOT.$filePath.$file))
		{
			return $filePath.$file;
		}
		else if(file_exists(DOCROOT.$filePath.$file1))
		{

			return $filePath.$file1;
		}
		else
		{
			return false;
		}
	}

	public static function bbcode2html($text,$brush='bash') {
		$text = trim($text);

		// BBCode [code]
		if (!function_exists('escape')) {
			function escape($s) {
				$code = $s[1];
				$code = htmlspecialchars($code);
				$code = str_replace("[", "&#91;", $code);
				$code = str_replace("]", "&#93;", $code);
				return '<pre class="brush:lzyyspecialbrush;">'.$code.'</pre>';
			}	
		}
		$text = preg_replace_callback('/\[code\](.*?)\[\/code\]/ms', "escape", $text);
		$text = str_replace('lzyyspecialbrush',$brush,$text);
		
		// BBCode to find...
		$in = array( 	 '/\[b\](.*?)\[\/b\]/ms',	
						 '/\[i\](.*?)\[\/i\]/ms',
						 '/\[u\](.*?)\[\/u\]/ms',
						 '/\[img\](.*?)\[\/img\]/ms',
						 '/\[email\](.*?)\[\/email\]/ms',
						 '/\[url\="?(.*?)"?\](.*?)\[\/url\]/ms',
						 '/\[size\="?(.*?)"?\](.*?)\[\/size\]/ms',
						 '/\[color\="?(.*?)"?\](.*?)\[\/color\]/ms',
						 '/\[quote](.*?)\[\/quote\]/ms',
						 '/\[list\=(.*?)\](.*?)\[\/list\]/ms',
						 '/\[list\](.*?)\[\/list\]/ms',
						 '/\[\*\]\s?(.*?)\n/ms'
		);
		// And replace them by...
		$out = array(	 '<strong>\1</strong>',
						 '<em>\1</em>',
						 '<u>\1</u>',
						 '<img src="\1" alt="\1" />',
						 '<a href="mailto:\1">\1</a>',
						 '<a href="\1">\2</a>',
						 '<span style="font-size:\1%">\2</span>',
						 '<span style="color:\1">\2</span>',
						 '<blockquote>\1</blockquote>',
						 '<ol start="\1">\2</ol>',
						 '<ul>\1</ul>',
						 '<li>\1</li>'
		);
		$text = preg_replace($in, $out, $text);
		
		// paragraphs
		$text = str_replace("\r", "", $text);
		$text = "<p>".ereg_replace("(\n){2,}", "</p><p>", $text)."</p>";
		$text = nl2br($text);
		
		// clean some tags to remain strict
		// not very elegant, but it works. No time to do better ;)
		if (!function_exists('removeBr')) {
			function removeBr($s) {
				return str_replace("<br />", "", $s[0]);
			}
		}	
		$text = preg_replace_callback('/<pre[^>]*>(.*?)<\/pre>/ms', "removeBr", $text);
		$text = preg_replace('/<p><pre([^>]*)>(.*?)<\/pre><\/p>/ms', "<pre\\1>\\2</pre>", $text);
		
		$text = preg_replace_callback('/<ul>(.*?)<\/ul>/ms', "removeBr", $text);
		$text = preg_replace('/<p><ul>(.*?)<\/ul><\/p>/ms', "<ul>\\1</ul>", $text);
		
		return $text;
	}
}
